home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 1
/
Atari Mega Archive - Volume 1.iso
/
gnu
/
gxx
/
gpincl20.zoo
/
streambu.h
< prev
next >
Wrap
C/C++ Source or Header
|
1993-07-13
|
19KB
|
585 lines
// This is part of the iostream library, providing -*- C++ -*- input/output.
// Copyright (C) 1991 Per Bothner.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
#ifndef _STREAMBUF_H
#define _STREAMBUF_H
#ifdef __GNUG__
#pragma interface
#endif
/* #define _G_IO_THROW */ /* Not implemented: ios::failure */
#include <g_config.h>
#ifdef _G_NEED_STDARG_H
#include <stdarg.h>
#endif
#ifndef atarist
#ifndef EOF
#define EOF (-1)
#endif
#ifndef NULL
#ifdef __GNUC__
#define NULL ((void*)0)
#else
#define NULL (0)
#endif
#endif
#else
#include <stddef.h>
#include <stdio.h>
extern "C" unsigned long __DEFAULT_BUFSIZ__;
#endif
class ostream; class streambuf; class backupbuf;
// In case some header files defines these as macros.
#undef open
#undef close
#ifdef _G_FRIEND_BUG
extern int __UNDERFLOW(streambuf*);
extern int __OVERFLOW(streambuf*, int);
#endif
extern "C" int __underflow(streambuf*);
extern "C" int __overflow(streambuf*, int);
typedef _G_off_t streamoff;
typedef _G_off_t streampos; // Should perhaps be _G_fpos_t ?
typedef unsigned long __fmtflags;
typedef unsigned char __iostate;
struct _ios_fields { // The data members of an ios.
streambuf *_strbuf;
ostream* _tie;
int _width;
__fmtflags _flags;
_G_wchar_t _fill;
__iostate _state;
__iostate _exceptions;
int _precision;
};
#define _IOS_GOOD 0
#define _IOS_EOF 1
#define _IOS_FAIL 2
#define _IOS_BAD 4
#define _IOS_INPUT 1
#define _IOS_OUTPUT 2
#define _IOS_ATEND 4
#define _IOS_APPEND 8
#define _IOS_TRUNC 16
#define _IOS_NOCREATE 32
#define _IOS_NOREPLACE 64
#define _IOS_BIN 128
#ifdef atarist
/* force text mode */
#define _IOS_TEXT 256
#endif
#ifdef _STREAM_COMPAT
enum state_value {
_good = _IOS_GOOD,
_eof = _IOS_EOF,
_fail = _IOS_FAIL,
_bad = _IOS_BAD };
enum open_mode {
input = _IOS_INPUT,
output = _IOS_OUTPUT,
atend = _IOS_ATEND,
append = _IOS_APPEND
#ifdef atarist
,
in_bin =_IOS_INPUT|_IOS_BIN, // force bin mode regardless of default mode
out_bin=_IOS_OUTPUT|_IOS_BIN,
app_bin=_IOS_APPEND|_IOS_BIN,
binary = _IOS_BIN, // flag that may be or'ed in
in_text=_IOS_INPUT|_IOS_TEXT, // force text mode
out_text=_IOS_OUTPUT|_IOS_TEXT,
app_text=_IOS_APPEND|_IOS_TEXT,
text=_IOS_TEXT // flag that may be or'ed in
#endif
};
#endif
class ios : public _ios_fields {
public:
typedef __fmtflags fmtflags;
typedef int iostate;
typedef int openmode;
typedef int streamsize;
enum io_state {
goodbit = _IOS_GOOD,
eofbit = _IOS_EOF,
failbit = _IOS_FAIL,
badbit = _IOS_BAD };
enum open_mode {
in = _IOS_INPUT,
out = _IOS_OUTPUT,
ate = _IOS_ATEND,
app = _IOS_APPEND,
trunc = _IOS_TRUNC,
nocreate = _IOS_NOCREATE,
noreplace = _IOS_NOREPLACE,
bin = _IOS_BIN
#ifdef atarist
,
in_bin = _IOS_INPUT|_IOS_BIN,
out_bin = _IOS_OUTPUT|_IOS_BIN,
ate_bin = _IOS_ATEND|_IOS_BIN,
app_bin = _IOS_APPEND|_IOS_BIN,
binary = _IOS_BIN,
in_text = _IOS_INPUT|_IOS_TEXT,
out_text = _IOS_OUTPUT|_IOS_TEXT,
ate_text = _IOS_ATEND |_IOS_TEXT,
app_text = _IOS_APPEND|_IOS_TEXT,
text = _IOS_TEXT
#endif
};
enum seek_dir { beg, cur, end};
// ANSI: typedef enum seek_dir seekdir; etc
enum { skipws=01, left=02, right=04, internal=010,
dec=020, oct=040, hex=0100,
showbase=0200, showpoint=0400, uppercase=01000, showpos=02000,
scientific=04000, fixed=010000, unitbuf=020000, stdio=040000,
dont_close=0100000 //Don't delete streambuf on stream destruction
};
enum { // Masks.
basefield=dec+oct+hex,
floatfield = scientific+fixed,
adjustfield = left+right+internal
};
#ifdef _G_IO_THROW
class failure : public xmsg {
ios* _stream;
public:
failure(ios* stream) { _stream = stream; }
failure(string cause, ios* stream) { _stream = stream; }
ios* rdios() const { return _stream; }
};
#endif
ostream* tie() const { return _tie; }
ostream* tie(ostream* val) { ostream* save=_tie; _tie=val; return save; }
// Methods to change the format state.
_G_wchar_t fill() const { return (_G_wchar_t)_fill; }
_G_wchar_t fill(_G_wchar_t newf)
{_G_wchar_t oldf = (_G_wchar_t)_fill; _fill = (char)newf; return oldf;}
fmtflags flags() const { return _flags; }
fmtflags flags(fmtflags new_val) {
fmtflags old_val = _flags; _flags = new_val; return old_val; }
int precision() const { return _precision; }
int precision(int newp) {
unsigned short oldp = _precision; _precision = (unsigned short)newp;
return oldp; }
fmtflags setf(fmtflags val) {
fmtflags oldbits = _flags;
_flags |= val; return oldbits; }
fmtflags setf(fmtflags val, fmtflags mask) {
fmtflags oldbits = _flags;
_flags = (_flags & ~mask) | (val & mask); return oldbits; }
fmtflags unsetf(fmtflags mask) {
fmtflags oldbits = _flags & mask;
_flags &= ~mask; return oldbits; }
int width() const { return _width; }
int width(int val) { int save = _width; _width = val; return save; }
#ifdef _G_IO_THROW
void _throw_failure() { throw new ios::failure(this); }
#else
void _throw_failure() { }
#endif
streambuf* rdbuf() const { return _strbuf; }
void clear(iostate state = 0) {
_state = _strbuf ? state : state|badbit;
if (_state & _exceptions) _throw_failure(); }
void set(iostate flag) { _state |= flag;
if (_state & _exceptions) _throw_failure(); }
void setstate(iostate flag) { _state |= flag; // ANSI
if (_state & _exceptions) _throw_failure(); }
int good() const { return _state == 0; }
int eof() const { return _state & ios::eofbit; }
int fail() const { return _state & (ios::badbit|ios::failbit); }
int bad() const { return _state & ios::badbit; }
iostate rdstate() const { return _state; }
operator void*() const { return fail() ? (void*)0 : (void*)(-1); }
int operator!() const { return fail(); }
iostate exceptions() const { return _exceptions; }
void exceptions(iostate enable) {
_exceptions = enable;
if (_state & _exceptions) _throw_failure(); }
static int sync_with_stdio(int on);
static void sync_with_stdio() { sync_with_stdio(1); }
#ifdef _STREAM_COMPAT
void unset(state_value flag) { _state &= ~flag; }
void close();
int is_open();
int readable();
int writable();
#endif
// Used to initialize standard streams. Not needed in this implementation.
class Init {
public:
Init () { }
};
protected:
ios(streambuf* sb = 0, ostream* tie = 0);
virtual ~ios();
void init(streambuf* sb) { _state=0; _strbuf=sb; }
};
#if __GNUG__==1
typedef int _seek_dir;
#else
typedef ios::seek_dir _seek_dir;
#endif
// Magic numbers and bits for the _flags field.
// The magic numbers use the high-order bits of _flags;
// the remaining bits are abailable for variable flags.
// Note: The magic numbers must all be negative if stdio
// emulation is desired.
#define _IO_MAGIC 0xFBAD0000 /* Magic number */
#define _OLD_STDIO_MAGIC 0xFABC0000 /* Emulate old stdio. */
#define _IO_MAGIC_MASK 0xFFFF0000
#define _S_USER_BUF 1 /* User owns buffer; don't delete it on close. */
#define _S_UNBUFFERED 2
#define _S_NO_READS 4 /* Reading not allowed */
#define _S_NO_WRITES 8 /* Writing not allowd */
#define _S_EOF_SEEN 0x10
#define _S_ERR_SEEN 0x20
#define _S_DELETE_DONT_CLOSE 0x40
#define _S_LINKED 0x80 // Set if linked (using _chain) to streambuf::_list_all.
#define _S_IN_BACKUP 0x100
#define _S_LINE_BUF 0x200
#define _S_TIED_PUT_GET 0x400 // Set if put and get pointer logicly tied.
#define _S_CURRENTLY_PUTTING 0x800
#define _S_IS_APPENDING 0x1000
#define _S_IS_BACKUPBUF 0x